package com.bsy.framework.task

import android.app.Application
import com.bsy.framework.BuildConfig
import com.bsy.framework.log.LogUtil
import com.bsy.stater.task.Task
import com.bsy.stater.utils.DispatcherExecutor
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVLogLevel
import java.util.concurrent.ExecutorService

/**
 * Created by Sincerity on 2024/3/5.
 * Copyright
 * 描述：MMKV初始化
 */
class MMKVTask (private val  application: Application): Task() {
    //异步线程执行的Task在被调用await的时候等待
    override fun needWait(): Boolean {
        return true
    }

    //依赖某些任务，在某些任务完成后才能执行
    override fun dependsOn(): MutableList<Class<out Task>> {
        val tasks = mutableListOf<Class<out Task?>>()
        tasks.add(APPHelperTask::class.java)
        return tasks
    }

    //指定线程池
    override fun runOn(): ExecutorService? {
        return DispatcherExecutor.iOExecutor
    }

    //执行任务初始化
    override fun run() {
        val rootDir: String = MMKV.initialize(application.baseContext)
        MMKV.setLogLevel(
            if (BuildConfig.DEBUG) {
                MMKVLogLevel.LevelDebug
            } else {
                MMKVLogLevel.LevelError
            }
        )
        LogUtil.d("mmkv root: $rootDir", tag = "MMKV")
    }
}